package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPrProfessionalTeamBinding;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.view.ui.adapter.home.ViewPagerAdapter;
import com.sgb.kjwl.viewmodel.enterprise.PrProfessionalTeamModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索界面大厅fragment
 */
public class PrProfessionalTeamFragment extends AppLazyFragment {

    private FragmentPrProfessionalTeamBinding mBinding;
    public PrProfessionalTeamModel mModel;

    public static PrProfessionalTeamFragment newInstance() {
        PrProfessionalTeamFragment fragment = new PrProfessionalTeamFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_pr_professional_team, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
    }

    @Override
    protected void initView() {
        mModel = new ViewModelProvider(getActivity()).get(PrProfessionalTeamModel.class);
        mBinding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.item_lin_2, null);
                TextView selectTextView = linearLayout.findViewById(R.id.tab_item_textview);
                selectTextView.setText(tab.getText());
                tab.setCustomView(linearLayout);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mModel.getServiceType();
        initObsever();
    }

    private void initObsever() {
        mModel.titleList.observe(this, new Observer<List<IpPickSelectEntity>>() {
            @Override
            public void onChanged(List<IpPickSelectEntity> ipPickSelectEntities) {
                List<Pair<String, Fragment>> mList = new ArrayList<>();
                for (IpPickSelectEntity s : ipPickSelectEntities) {
                 /*   Bundle bundle = new Bundle();
                    ProfessionalTeamFragment2 couponFragment = new ProfessionalTeamFragment2();
                    bundle.putString("code", s.getValue());
                    couponFragment.setArguments(bundle);*/
                    mList.add(new Pair<>(s.getName(), ProfessionalTeamFragment2.newInstance(s.getValue())));
                }
                mBinding.vpProfessionalTeam.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), mList));
                mBinding.vpProfessionalTeam.setOffscreenPageLimit(ipPickSelectEntities.size());
                mBinding.tabLayout.setupWithViewPager(mBinding.vpProfessionalTeam);
                mBinding.tabLayout.getTabAt(0).select();
            }
        });
    }


}